function varargout = Open(varargin)
% OPEN MATLAB code for Open.fig
%      OPEN, by itself, creates a new OPEN or raises the existing
%      singleton*.
%
%      H = OPEN returns the handle to a new OPEN or the handle to
%      the existing singleton*.
%
%      OPEN('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in OPEN.M with the given input arguments.
%
%      OPEN('Property','Value',...) creates a new OPEN or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Open_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Open_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help Open

% Last Modified by GUIDE v2.5 23-Jun-2015 13:40:49

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Open_OpeningFcn, ...
                   'gui_OutputFcn',  @Open_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before Open is made visible.
function Open_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to Open (see VARARGIN)

% Choose default command line output for Open
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);
set(handles.imageaxes, 'Visible', 'on');
axes(handles.imageaxes);
axis off
setappdata(handles.output,'RS',[]);
setappdata(handles.output,'FileName',[]);
% UIWAIT makes Open wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = Open_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in openbutton.
function openbutton_Callback(hObject, eventdata, handles)
% hObject    handle to openbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
try
    [FileName,PathName] = uigetfile({'*.*';'.hdf';'.HDR'},...
        'Select ENVI/MODIS/MERIS data','Inputs/Sardegna/Pre_img');
    [RSdata,RSdataSize] =loadEnviData([PathName FileName]);
    
    if ~isempty(RSdata)
        set(handles.fileaddresstext,'String', [PathName FileName]);
        set(handles.bandnumbertext,'String',num2str(RSdataSize(3)));
        set(handles.rowsnumbertext,'String',num2str(RSdataSize(1)));
        set(handles.colsnumbertext,'String',num2str(RSdataSize(2)));
        set(handles.grayedit,'String','1');
        axes(handles.imageaxes);
        imshow(mat2gray(RSdata(:,:,1)));
        axis off;
        setappdata(handles.output, 'RS', RSdata);
        setappdata(handles.output,'FileName',FileName);
    else
        
        set(handles.fileaddresstext,'String', 'DATA ERROR!');
        set(handles.bandnumbertext,'String','');
        set(handles.rowsnumbertext,'String','');
        set(handles.colsnumbertext,'String','');

    end
    
catch
    try
        [RSdata,RSdataSize] =loadModis1BData([PathName FileName]);
        if ~isempty(RSdata)
        set(handles.fileaddresstext,'String', [PathName FileName]);
        set(handles.bandnumbertext,'String',num2str(RSdataSize(3)));
        set(handles.rowsnumbertext,'String',num2str(RSdataSize(1)));
        set(handles.colsnumbertext,'String',num2str(RSdataSize(2)));
        set(handles.grayedit,'String','1');
        axes(handles.imageaxes);
        imshow(mat2gray(RSdata(:,:,1)));
        axis off;
        setappdata(handles.output, 'RS', RSdata);
        setappdata(handles.output,'FileName',FileName);
        else
        
        set(handles.fileaddresstext,'String', 'DATA ERROR!');
        set(handles.bandnumbertext,'String','');
        set(handles.rowsnumbertext,'String','');
        set(handles.colsnumbertext,'String','');

        end
    catch
    set(handles.fileaddresstext,'String', 'NOT ENVI/MODIS/MERIS DATA!');
    set(handles.bandnumbertext,'String','');
    set(handles.rowsnumbertext,'String','');
    set(handles.colsnumbertext,'String','');
    end
end




% --- Executes on button press in graybutton.
function graybutton_Callback(hObject, eventdata, handles)
% hObject    handle to graybutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of graybutton


% --- Executes on button press in RGBbutton.
function RGBbutton_Callback(hObject, eventdata, handles)
% hObject    handle to RGBbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of RGBbutton



function Redit_Callback(hObject, eventdata, handles)
% hObject    handle to Redit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Redit as text
%        str2double(get(hObject,'String')) returns contents of Redit as a double


% --- Executes during object creation, after setting all properties.
function Redit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Redit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function Gedit_Callback(hObject, eventdata, handles)
% hObject    handle to Gedit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Gedit as text
%        str2double(get(hObject,'String')) returns contents of Gedit as a double


% --- Executes during object creation, after setting all properties.
function Gedit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Gedit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function Bedit_Callback(hObject, eventdata, handles)
% hObject    handle to Bedit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Bedit as text
%        str2double(get(hObject,'String')) returns contents of Bedit as a double


% --- Executes during object creation, after setting all properties.
function Bedit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Bedit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function grayedit_Callback(hObject, eventdata, handles)
% hObject    handle to grayedit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of grayedit as text
%        str2double(get(hObject,'String')) returns contents of grayedit as a double


% --- Executes during object creation, after setting all properties.
function grayedit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to grayedit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in showbutton.
function showbutton_Callback(hObject, eventdata, handles)
% hObject    handle to showbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
RSdata = getappdata(handles.output,'RS');
if ~isempty(RSdata)
    if get(handles.graybutton, 'Value') == 1
        [graybandnumber, status] = str2num(get(handles.grayedit,'String'));
        if status == 1
%             RSdata = getappdata(handles.output,'RS');
            axes(handles.imageaxes);
            imshow(mat2gray(RSdata(:,:,graybandnumber)));
            axis off;
        end
    else 
         image=normalize(RSdata);
         imageRGB = showRGBImage( image,[str2num(get(handles.Redit,'String')), ...
             str2num(get(handles.Gedit,'String')), str2num(get(handles.Bedit,'String'))]);
         axes(handles.imageaxes);
         imshow(imageRGB,[]);
         axis off;
    end
end


% --- Executes on button press in clusterbutton.
function clusterbutton_Callback(hObject, eventdata, handles)
% hObject    handle to clusterbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
Clusting(getappdata(handles.output,'RS'),getappdata(handles.output,'FileName'));


% --- Executes on button press in cdbutton.
function cdbutton_Callback(hObject, eventdata, handles)
% hObject    handle to cdbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
changeDetectionUI();


% --- Executes on button press in classificationbutton.
function classificationbutton_Callback(hObject, eventdata, handles)
% hObject    handle to classificationbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
Classification(getappdata(handles.output,'RS'));


% --- Executes on button press in ROIbutton.
function ROIbutton_Callback(hObject, eventdata, handles)
% hObject    handle to ROIbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in zoominbutton.
function zoominbutton_Callback(hObject, eventdata, handles)
% hObject    handle to zoominbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
h = zoom;
setAxesZoomMotion(h,handles.imageaxes,'both');%horizontal?vertical???
zoom on
set(h,'direction','in');

% --- Executes on button press in zoomoutbutton11.
function zoomoutbutton11_Callback(hObject, eventdata, handles)
% hObject    handle to zoomoutbutton11 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
h = zoom;
setAxesZoomMotion(h,handles.imageaxes,'both');%horizontal?vertical???
zoom on
set(h,'direction','out');


% --- Executes on button press in fearturelearningbutton.
function fearturelearningbutton_Callback(hObject, eventdata, handles)
% hObject    handle to fearturelearningbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
FeatureLearning(getappdata(handles.output,'RS'),getappdata(handles.output,'FileName'));
